package com.it.reggie.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.it.reggie.Mapper.DishMapper;
import com.it.reggie.Service.CategoryService;
import com.it.reggie.Service.DishFlavorService;
import com.it.reggie.Service.DishService;
import com.it.reggie.dto.DishDto;
import com.it.reggie.entity.Category;
import com.it.reggie.entity.Dish;
import com.it.reggie.entity.DishFlavor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    @Override
    public int queryDishes(Long id) {
        int i = dishMapper.queryDishes(id);
        return i;
    }

    /**
     * 新增菜品，同时保存对应的菜品口味数据到口味表
     * @param dishDto
     */

    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品到菜品表
        this.save(dishDto);

        Long dishId = dishDto.getCategoryId();


        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item)->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表
        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 根据id 查询菜品信息和对应口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByteWithFlavor(Long id) {

        //菜品信息
        Dish dish = this.getById(id);
        //口味信息 口味表查询

        DishDto dishDto = new DishDto();

        BeanUtils.copyProperties(dish,dishDto);

        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> Flavor = dishFlavorService.list(lambdaQueryWrapper);
        dishDto.setFlavors(Flavor);
        return dishDto;
    }

    @Override
    public void updataWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);
        //清理当前菜品对应口味数据 -- dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> LambdaQueryWrapper = new LambdaQueryWrapper<>();

        LambdaQueryWrapper.eq(DishFlavor::getDishId,dishDto.getId());

        dishFlavorService.remove(LambdaQueryWrapper);

        //添加当前提交过来的口味数据 --dish_flavor表的insert操作

        List<DishFlavor> flavors = dishDto.getFlavors();

        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
            dishFlavorService.save(flavor);
        }

    }

    @Override
    public List<DishDto> listMeal(Long categoryId) {

        List<Dish> dish = dishMapper.listMeal(categoryId);



        List<DishDto> dishDtoList =  dish.stream().map((item)-> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);

            Long categoryid = item.getCategoryId(); //分类id
            //根据id查询分类id
            Category category = categoryService.getById(categoryid);

            if (category != null) {
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            //菜品id
            Long dishid = item.getId();

            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();

            //SQL select * from dish_flavor where dish_id = ?
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dishid);

            List<DishFlavor> DishFlavorlist = dishFlavorService.list(dishFlavorLambdaQueryWrapper);

            dishDto.setFlavors(DishFlavorlist);

            return dishDto;
        }).collect(Collectors.toList());



        return dishDtoList;
    }


}
